﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public class PlumbingAutoDesignLogic
	{
		public PlumbingAutoDesignLogic(IList<Element> elemSet)
		{
			this.listElem = elemSet;
		}

		public string ExamineElements()
		{
			if (this.listElem == null || this.listElem.Count <= 0)
			{
				return "没有选中任何实体，请重新选择设备……";
			}
			List<Pipe> list = new List<Pipe>();
			foreach (Element element in this.listElem)
			{
				if (element is Pipe)
				{
					list.Add(element as Pipe);
				}
				if (element is FamilyInstance)
				{
					ConnectorManager connectorManager = (element as FamilyInstance).MEPModel.ConnectorManager;
					if (connectorManager != null)
					{
						ConnectorSet connectors = connectorManager.Connectors;
						if (connectors != null && connectors.Size > 0)
						{
							ConnectorSetIterator connectorSetIterator = connectors.ForwardIterator();
							int num = 0;
							while (connectorSetIterator.MoveNext())
							{
								object obj = connectorSetIterator.Current;
								Connector connector = obj as Connector;
								if (connector != null && 3 == (int)connector.Domain && (int)connector.Direction == 2)
								{
									num++;
								}
							}
							if (num > 0)
							{
								this.listFamilyInstance.Add(element as FamilyInstance);
							}
						}
					}
				}
			}
			this.doc = this.listElem[0].Document;
			double elevation = this.doc.ActiveView.GenLevel.Elevation;
			if (list.Count < 1)
			{
				return "选择实体集合中必须包含一个水管立管，\n请重新选择设计用实体集合……";
			}
			foreach (Pipe pipe in list)
			{
				XYZ[] pipePoint = this.GetPipePoint(pipe);
				if (this.BetweenTwoNums(pipePoint[0].Z, pipePoint[1].Z, elevation))
				{
					this.riserPipe = pipe;
					break;
				}
			}
			if (this.riserPipe == null)
			{
				return "选择实体集合中必须包含一个水管立管，\n请重新选择设计用实体集合……";
			}
			if (this.listFamilyInstance.Count <= 0)
			{
				return "选择实体集合中至少包含一个卫生设备，\n请重新选择设计用实体集合……";
			}
			this.pipePnt = this.GetPipeWorkPlanPoint();
			this.InitialParameter();
			return "";
		}

		private void InitialParameter()
		{
			this.offset = -600.0;
			this.slope = 0.012;
			this.pipeDiameter = 100.0;
			this.zValue = this.doc.ActiveView.GenLevel.Elevation;
			HYDrainageSystemSetting instance = HYDrainageSystemSetting.Instance;
			instance.InitialPipeSystemType(this.doc);
			this.pipeType = instance.HYPipeType;
			this.psType = this.GetPipingSystemType();
			this.fsTee = instance.GetSettedFamilySymbol(this.doc, HYFamilySymbolSign.Tee);
			if (this.fsTee.Family.Name.Contains("顺水"))
			{
				this.isDownTee = true;
			}
		}

		private List<List<DesignFamily>> GetPipeLineDic()
		{
			IEnumerable<List<FamilyInstance>> enumerable = this.SortFamilyByHost();
			double multiple = 4.0;
			if (Math.Abs(this.doc.ActiveView.GenLevel.Elevation) > 0.5)
			{
				multiple = 6.0;
			}
			List<List<DesignFamily>> list = new List<List<DesignFamily>>();
			foreach (List<FamilyInstance> list2 in enumerable)
			{
				List<DesignFamily> list3 = new List<DesignFamily>();
				Line pipeLine = this.GetPipeLine(list2, multiple);
				foreach (FamilyInstance hyFamilyInstance in list2)
				{
					list3.Add(new DesignFamily
					{
						HyFamilyInstance = hyFamilyInstance,
						PipeLine = pipeLine
					});
				}
				list.Add(list3);
			}
			return list;
		}

		public bool CreatHorizontalPipe()
		{
			List<List<DesignFamily>> pipeLineDic = this.GetPipeLineDic();
			if (pipeLineDic.Count == 1)
			{
				return this.DealWithOnlyPipe(pipeLineDic[0]);
			}
			if (pipeLineDic.Count == 2)
			{
				return this.DealWithSecondKind(pipeLineDic);
			}
			return pipeLineDic.Count == 3 && this.DealWithThreePipes(pipeLineDic);
		}

		private bool DealWithOnlyPipe(List<DesignFamily> dfSet)
		{
			List<DesignFamily> list = this.JudgeFamilyByPoint(this.pipePnt, dfSet[0].PipeLine, this.GetFamilyList(dfSet), true);
			if (list == null)
			{
				return false;
			}
			string text = "";
			string text2 = "";
			XYZ[] pipePoint = this.GetPipePoint(list, true);
			Pipe pipe = this.AutoCreatPipe(this.offset, pipePoint[0], pipePoint[1], this.pipeDiameter, this.slope);
			new YJKPipeConnectorHelper().TPipeConnectRPipe(this.riserPipe, pipe, this.doc, this.fsTee, this.isDownTee, 4, ref text, false);
			if (!string.IsNullOrEmpty(text))
			{
				text2 = text;
			}
			new AutoDesignHelper().PipeConnectEquipmentBatch(this.GetFamilyList(list), pipe, ref text);
			if (!string.IsNullOrEmpty(text))
			{
				text2 = text2 + "\n" + text;
			}
			if (!string.IsNullOrEmpty(text2))
			{
				text2 = "部分连接由于位置关系，\n没有办法自动连接，\n请手动处理……";
				YJKMessageBox.Information(text2);
			}
			return true;
		}

		private bool DealWithSecondKind(List<List<DesignFamily>> dfSet)
		{
			if (dfSet.Count != 2)
			{
				return false;
			}
			List<DesignFamily> list = dfSet[0];
			List<DesignFamily> list2 = dfSet[1];
			XYZ xyz = this.JudgeLineRelation(list[0].PipeLine, list2[1].PipeLine);
			double num = list[0].PipeLine.Distance(this.pipePnt);
			double num2 = list2[0].PipeLine.Distance(this.pipePnt);
			if (xyz != null)
			{
				if (num > num2)
				{
					this.IntersectPipes(list2, list, this.pipePnt, xyz, this.offset);
				}
				else
				{
					this.IntersectPipes(list, list2, this.pipePnt, xyz, this.offset);
				}
			}
			else if (num > num2)
			{
				this.ParallelPipes(list2, list, this.pipePnt);
			}
			else
			{
				this.ParallelPipes(list, list2, this.pipePnt);
			}
			return true;
		}

		private bool DealWithThreePipes(List<List<DesignFamily>> dfSet)
		{
			if (dfSet.Count != 3)
			{
				return false;
			}
			List<DesignFamily>[] array = this.CheckAndOrderDesignFamily(dfSet);
			if (array == null)
			{
				return false;
			}
			XYZ xyz = this.JudgeLineRelation(array[0][0].PipeLine, array[1][0].PipeLine);
			XYZ xyz2 = this.JudgeLineRelation(array[1][0].PipeLine, array[2][0].PipeLine);
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			string text = "";
			List<DesignFamily> list = this.JudgeFamilyByPoint(this.pipePnt, array[0][0].PipeLine, this.GetFamilyList(array[0]), true);
			if (list == null)
			{
				return false;
			}
			List<DesignFamily> list2 = this.JudgeFamilyByPoint(xyz, array[1][0].PipeLine, this.GetFamilyList(array[1]), true);
			if (list2 == null)
			{
				return false;
			}
			List<DesignFamily> list3 = this.JudgeFamilyByPoint(xyz2, array[2][0].PipeLine, this.GetFamilyList(array[2]), true);
			if (list3 == null)
			{
				return false;
			}
			XYZ[] pipePoint = this.GetPipePoint(list, true);
			if (pipePoint == null)
			{
				return false;
			}
			AutoDesignHelper autoDesignHelper = new AutoDesignHelper();
			Pipe pipe = this.AutoCreatPipe(this.offset, pipePoint[0], xyz, this.pipeDiameter, this.slope);
			double num = pipePoint[0].DistanceTo(xyz) * Math.Tan(this.slope) * 304.8;
			if (this.GetPipePoint(list2, true) == null)
			{
				return false;
			}
			Pipe pipe2 = this.AutoCreatPipe(this.offset + num, xyz, xyz2, this.pipeDiameter, this.slope);
			XYZ[] pipePoint2 = this.GetPipePoint(list3, true);
			if (pipePoint2 == null)
			{
				return false;
			}
			num += xyz.DistanceTo(pipePoint2[1]) * Math.Tan(this.slope) * 304.8;
			Pipe pipe3 = this.AutoCreatPipe(this.offset + num, xyz2, pipePoint2[1], this.pipeDiameter, this.slope);
			List<Pipe> list4 = autoDesignHelper.PipeConnectEquipmentBatch(this.GetFamilyList(list), pipe, ref text);
			List<Pipe> list5 = autoDesignHelper.PipeConnectEquipmentBatch(this.GetFamilyList(list2), pipe2, ref text);
			List<Pipe> list6 = autoDesignHelper.PipeConnectEquipmentBatch(this.GetFamilyList(list3), pipe3, ref text);
			if (list4 != null && list4.Count == 2)
			{
				List<Pipe> pipeByBasePoint = this.GetPipeByBasePoint(pipePoint[0], list4);
				Pipe pipe4 = pipeByBasePoint[0];
				hypipeConnectorHelper.TPipeConnectRPipe(this.riserPipe, pipe4, this.doc, this.fsTee, this.isDownTee, 4, ref text, false);
				Pipe pipe5 = pipeByBasePoint[1];
				if (list5 != null && list5.Count == 2)
				{
					List<Pipe> pipeByBasePoint2 = this.GetPipeByBasePoint(xyz, list5);
					Pipe pipe6 = pipeByBasePoint2[0];
					hypipeConnectorHelper.connect45Elbow(pipe5, this.GetPipeMiddlePoint(pipe5), pipe6, this.GetPipeMiddlePoint(pipe6), this.doc);
					Pipe pipe7 = pipeByBasePoint2[1];
					if (list6 != null && list6.Count == 1)
					{
						hypipeConnectorHelper.connect45Elbow(pipe7, this.GetPipeMiddlePoint(pipe7), list6[0], this.GetPipeMiddlePoint(list6[0]), this.doc);
					}
				}
			}
			return true;
		}

		private List<DesignFamily>[] CheckAndOrderDesignFamily(List<List<DesignFamily>> dfSet)
		{
			List<DesignFamily>[] array = new List<DesignFamily>[3];
			double num = 0.0;
			List<DesignFamily> list = null;
			for (int i = 0; i < dfSet.Count; i++)
			{
				if (i == 0)
				{
					num = dfSet[i][0].PipeLine.Distance(this.pipePnt);
					list = dfSet[i];
				}
				else if (num > dfSet[i][0].PipeLine.Distance(this.pipePnt))
				{
					list = dfSet[i];
				}
			}
			array[0] = list;
			dfSet.Remove(list);
			List<DesignFamily> list2 = null;
			foreach (List<DesignFamily> list3 in dfSet)
			{
				if (this.JudgeLineRelation(list[0].PipeLine, list3[0].PipeLine) != null)
				{
					array[1] = list3;
					list2 = list3;
					break;
				}
			}
			if (list2 == null)
			{
				return null;
			}
			dfSet.Remove(list2);
			if (this.JudgeLineRelation(list[0].PipeLine, dfSet[0][0].PipeLine) == null)
			{
				array[2] = dfSet[0];
				return array;
			}
			return null;
		}

		private bool IntersectPipes(List<DesignFamily> firstDFSet, List<DesignFamily> secondDFSet, XYZ basePnt, XYZ intersectPnt, double levelOffset)
		{
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			AutoDesignHelper autoDesignHelper = new AutoDesignHelper();
			firstDFSet = this.JudgeFamilyByPoint(basePnt, firstDFSet[0].PipeLine, this.GetFamilyList(firstDFSet), true);
			if (firstDFSet == null)
			{
				return false;
			}
			secondDFSet = this.JudgeFamilyByPoint(intersectPnt, secondDFSet[0].PipeLine, this.GetFamilyList(secondDFSet), true);
			if (secondDFSet == null)
			{
				return false;
			}
			string text = "";
			XYZ[] pipePoint = this.GetPipePoint(firstDFSet, true);
			if (pipePoint == null)
			{
				return false;
			}
			Pipe pipe = this.AutoCreatPipe(levelOffset, pipePoint[0], intersectPnt, this.pipeDiameter, this.slope);
			double num = pipePoint[0].DistanceTo(intersectPnt) * Math.Tan(this.slope) * 304.8;
			XYZ[] pipePoint2 = this.GetPipePoint(secondDFSet, true);
			if (pipePoint2 == null)
			{
				return false;
			}
			Pipe pipe2 = this.AutoCreatPipe(levelOffset + num, intersectPnt, pipePoint2[1], this.pipeDiameter, this.slope);
			num += intersectPnt.DistanceTo(pipePoint2[1]) * Math.Tan(this.slope) * 304.8;
			List<Pipe> list = autoDesignHelper.PipeConnectEquipmentBatch(this.GetFamilyList(firstDFSet), pipe, ref text);
			List<Pipe> list2 = autoDesignHelper.PipeConnectEquipmentBatch(this.GetFamilyList(secondDFSet), pipe2, ref text);
			Pipe pipe3 = null;
			Pipe pipe4 = null;
			Pipe pipe5 = null;
			if (list != null && list.Count == 2)
			{
				List<Pipe> pipeByBasePoint = this.GetPipeByBasePoint(pipePoint[0], list);
				pipe3 = pipeByBasePoint[0];
				pipe4 = pipeByBasePoint[1];
			}
			if (list2 != null)
			{
				List<Pipe> pipeByBasePoint2 = this.GetPipeByBasePoint(intersectPnt, list2);
				pipe5 = pipeByBasePoint2[0];
				if (pipeByBasePoint2.Count == 2)
				{
					Pipe pipe6 = pipeByBasePoint2[1];
				}
			}
			hypipeConnectorHelper.TPipeConnectRPipe(this.riserPipe, pipe3, this.doc, this.fsTee, this.isDownTee, 4, ref text, false);
			hypipeConnectorHelper.connect45Elbow(pipe4, this.GetPipeMiddlePoint(pipe4), pipe5, this.GetPipeMiddlePoint(pipe5), this.doc);
			return true;
		}

		private List<Pipe> GetPipeByBasePoint(XYZ point, List<Pipe> pipes)
		{
			List<Pipe> list = new List<Pipe>();
			if (pipes == null || pipes.Count <= 0)
			{
				return null;
			}
			if (pipes.Count == 1)
			{
				list.Add(pipes[0]);
				return list;
			}
			if (pipes.Count == 2)
			{
				XYZ pipeMiddlePoint = this.GetPipeMiddlePoint(pipes[0]);
				XYZ pipeMiddlePoint2 = this.GetPipeMiddlePoint(pipes[1]);
				double num = point.DistanceTo(pipeMiddlePoint);
				double num2 = point.DistanceTo(pipeMiddlePoint2);
				if (num < num2)
				{
					list.Insert(0, pipes[0]);
					list.Insert(1, pipes[1]);
				}
				else
				{
					list.Insert(0, pipes[1]);
					list.Insert(1, pipes[0]);
				}
			}
			return list;
		}

		private bool ParallelPipes(List<DesignFamily> firstDFSet, List<DesignFamily> secondDFSet, XYZ basePnt)
		{
			YJKPipeConnectorHelper hypipeConnectorHelper = new YJKPipeConnectorHelper();
			AutoDesignHelper autoDesignHelper = new AutoDesignHelper();
			string text = "";
			XYZ parallelPoint = this.GetParallelPoint(firstDFSet, secondDFSet, basePnt);
			firstDFSet = this.JudgeFamilyByPoint(basePnt, firstDFSet[0].PipeLine, this.GetFamilyList(firstDFSet), true);
			if (firstDFSet == null)
			{
				return false;
			}
			secondDFSet = this.JudgeFamilyByPoint(parallelPoint, secondDFSet[0].PipeLine, this.GetFamilyList(secondDFSet), true);
			if (secondDFSet == null)
			{
				return false;
			}
			XYZ[] pipePoint = this.GetPipePoint(firstDFSet, true);
			Pipe pipe = this.AutoCreatPipe(this.offset, pipePoint[0], parallelPoint, this.pipeDiameter, this.slope);
			XYZ[] pipePoint2 = this.GetPipePoint(secondDFSet, true);
			double num = pipePoint[0].DistanceTo(parallelPoint) * Math.Tan(this.slope) * 304.8;
			Pipe pipe2 = this.AutoCreatPipe(this.offset + num, parallelPoint, pipePoint2[0], this.pipeDiameter, this.slope);
			num += parallelPoint.DistanceTo(pipePoint2[0]) * Math.Tan(this.slope) * 304.8;
			Pipe pipe3 = this.AutoCreatPipe(this.offset + num, pipePoint2[0], pipePoint2[1], this.pipeDiameter, this.slope);
			List<Pipe> pipes = autoDesignHelper.PipeConnectEquipmentBatch(this.GetFamilyList(firstDFSet), pipe, ref text);
			List<Pipe> list = autoDesignHelper.PipeConnectEquipmentBatch(this.GetFamilyList(secondDFSet), pipe3, ref text);
			List<Pipe> pipeByBasePoint = this.GetPipeByBasePoint(pipePoint[0], pipes);
			hypipeConnectorHelper.TPipeConnectRPipe(this.riserPipe, pipeByBasePoint[0], this.doc, this.fsTee, this.isDownTee, 4, ref text, false);
			hypipeConnectorHelper.connect45Elbow(pipeByBasePoint[1], this.GetPipeMiddlePoint(pipeByBasePoint[1]), pipe2, this.GetPipeMiddlePoint(pipe2), this.doc);
			hypipeConnectorHelper.connect45Elbow(pipe2, this.GetPipeMiddlePoint(pipe2), list[0], this.GetPipeMiddlePoint(list[0]), this.doc);
			return true;
		}

		private XYZ GetParallelPoint(List<DesignFamily> firstDFSet, List<DesignFamily> secondDFSet, XYZ basePnt)
		{
			Line pipeLine = firstDFSet[0].PipeLine;
			firstDFSet = this.JudgeFamilyByPoint(basePnt, firstDFSet[0].PipeLine, this.GetFamilyList(firstDFSet), true);
			if (firstDFSet == null)
			{
				return null;
			}
			secondDFSet = this.JudgeFamilyByPoint(basePnt, secondDFSet[0].PipeLine, this.GetFamilyList(secondDFSet), false);
			if (secondDFSet == null)
			{
				return null;
			}
			XYZ[] pipePoint = this.GetPipePoint(firstDFSet, true);
			XYZ[] pipePoint2 = this.GetPipePoint(secondDFSet, true);
			XYZ[] pipePoint3 = this.GetPipePoint(secondDFSet, false);
			double num = pipePoint[0].DistanceTo(pipePoint[1]);
			double num2 = pipePoint2[0].DistanceTo(pipePoint2[1]);
			double num3 = pipePoint3[0].DistanceTo(pipePoint3[1]);
			XYZ xyz = null;
			if (num < num3 || num > num2)
			{
				double num4 = pipePoint[0].DistanceTo(pipePoint[1]) + 1.9685039370078738;
				XYZ xyz2 = pipePoint[1].Subtract(pipePoint[0]).Normalize();
				xyz = pipePoint[0] + num4 * xyz2;
			}
			else if (num < num2)
			{
				double num4 = pipePoint2[0].DistanceTo(pipePoint2[1]) + 1.9685039370078738;
				XYZ xyz2 = pipePoint2[1].Subtract(pipePoint2[0]).Normalize();
				xyz = pipePoint2[0] + num4 * xyz2;
				xyz = pipeLine.Project(xyz).XYZPoint;
			}
			return xyz;
		}

		private XYZ GetPipeMiddlePoint(Pipe pipe)
		{
			Curve curve = (pipe.Location as LocationCurve).Curve;
			return curve.GetEndPoint(0).Add(curve.GetEndPoint(1)) / 2.0;
		}

		private XYZ[] GetPipePoint(List<DesignFamily> listFI, bool beFirst)
		{
			XYZ basePoint;
			XYZ projectPoint;
			if (beFirst)
			{
				basePoint = listFI.First<DesignFamily>().BasePoint;
				projectPoint = listFI.First<DesignFamily>().ProjectPoint;
			}
			else
			{
				basePoint = listFI.Last<DesignFamily>().BasePoint;
				projectPoint = listFI.Last<DesignFamily>().ProjectPoint;
			}
			return new XYZ[]
			{
				new XYZ(basePoint.X, basePoint.Y, this.zValue),
				new XYZ(projectPoint.X, projectPoint.Y, this.zValue)
			};
		}

		private Pipe AutoCreatPipe(double offset, XYZ sPoint, XYZ ePoint, double diameter, double slope)
		{
			return YJKRevitTools.createPipe(offset, sPoint, ePoint, diameter, slope, this.psType, this.pipeType, this.doc);
		}

		private PipingSystemType GetPipingSystemType()
		{
			PipingSystem pipingSystem = this.riserPipe.MEPSystem as PipingSystem;
			return this.doc.GetElementById(pipingSystem.GetTypeId()) as PipingSystemType;
		}

		private bool BetweenTwoNums(double firNum, double secNum, double judgeNum)
		{
			bool result = false;
			double num;
			double num2;
			if (firNum >= secNum)
			{
				num = firNum;
				num2 = secNum;
			}
			else
			{
				num = secNum;
				num2 = firNum;
			}
			if (judgeNum >= num2 && judgeNum <= num)
			{
				result = true;
			}
			return result;
		}

		private List<FamilyInstance> GetFamilyList(List<DesignFamily> listFamily)
		{
			List<FamilyInstance> list = new List<FamilyInstance>();
			foreach (DesignFamily designFamily in listFamily)
			{
				list.Add(designFamily.HyFamilyInstance);
			}
			return list;
		}

		private IList<List<FamilyInstance>> SortFamilyByHost()
		{
			IList<List<FamilyInstance>> list = new List<List<FamilyInstance>>();
			for (int i = 0; i < this.listFamilyInstance.Count; i++)
			{
				List<FamilyInstance> list2 = new List<FamilyInstance>();
				bool flag = false;
				foreach (List<FamilyInstance> fiList in list)
				{
					if (this.IsChecked(this.listFamilyInstance[i], fiList))
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					list2.Add(this.listFamilyInstance[i]);
					for (int j = i + 1; j < this.listFamilyInstance.Count; j++)
					{
						if (this.listFamilyInstance[i].Host.Id == this.listFamilyInstance[j].Host.Id)
						{
							list2.Add(this.listFamilyInstance[j]);
						}
					}
					list.Add(list2);
				}
			}
			return list;
		}

		private List<DesignFamily> JudgeFamilyByPoint(XYZ point, Line line, List<FamilyInstance> listFamily, bool beCheck)
		{
			double num = 0.0;
			XYZ xyzpoint = line.Project(point).XYZPoint;
			if (listFamily.Count <= 0)
			{
				YJKMessageBox.Information("请重新选择自动设计需要的实体列表……");
				return null;
			}
			List<DesignFamily> list = new List<DesignFamily>();
			if (listFamily.Count == 1)
			{
				XYZ xyzpoint2 = line.Project(this.GetFIConnPnt(listFamily[0], ref num)).XYZPoint;
				double baseDistance = xyzpoint.DistanceTo(xyzpoint2);
				list.Add(new DesignFamily
				{
					HyFamilyInstance = listFamily[0],
					ProjectPoint = xyzpoint2,
					BaseDistance = baseDistance,
					BasePoint = xyzpoint
				});
				return list;
			}
			XYZ xyz = null;
			for (int i = 0; i < listFamily.Count; i++)
			{
				XYZ xyzpoint2 = line.Project(this.GetFIConnPnt(listFamily[i], ref num)).XYZPoint;
				XYZ xyz2 = xyzpoint.Subtract(xyzpoint2).Normalize();
				double baseDistance = xyzpoint.DistanceTo(xyzpoint2);
				list.Add(new DesignFamily
				{
					HyFamilyInstance = listFamily[i],
					ProjectPoint = xyzpoint2,
					BaseDistance = baseDistance,
					BasePoint = xyzpoint
				});
				if (i == 0)
				{
					xyz = xyz2;
				}
				else if (!xyz2.IsAlmostEqualTo(xyz) && beCheck)
				{
					YJKMessageBox.Information("立管位置不正确，\n请重新设定立管后，\n再进行连接！");
					return null;
				}
			}
			list = (from r in list
			orderby r.BaseDistance descending
			select r).ToList<DesignFamily>();
			for (int j = 0; j < list.Count - 1; j++)
			{
				if (Math.Abs(list[j + 1].BaseDistance - list[j].BaseDistance) < 0.98425196850393692)
				{
					YJKMessageBox.Information("卫生器具位置不合适，\n请重新定位后，\n再进行连接！");
					return null;
				}
			}
			return list;
		}

		private XYZ JudgeLineRelation(Line line, Line otherLine)
		{
			return new YJKLine(line).IntersectionPoint(otherLine);
		}

		private bool IsChecked(FamilyInstance fi, List<FamilyInstance> fiList)
		{
			return fiList.Contains(fi);
		}

		private Line GetPipeLine(List<FamilyInstance> sortedFamilyInstance, double multiple)
		{
			Wall wall = sortedFamilyInstance[0].Host as Wall;
			Line wallLine = this.GetWallLine(this.GetWallPoint(wall));
			double num = 0.0;
			XYZ xyz = null;
			XYZ xyz2 = null;
			foreach (FamilyInstance fi in sortedFamilyInstance)
			{
				double num2 = 0.0;
				double num3 = 0.0;
				XYZ ficonnPnt = this.GetFIConnPnt(fi, ref num2);
				XYZ lineProjectPnt = this.GetLineProjectPnt(wallLine, ficonnPnt, ref num3);
				if (num3 + multiple * num2 > num)
				{
					num = num3 + multiple * num2;
					xyz = ficonnPnt;
					xyz2 = lineProjectPnt;
				}
			}
			XYZ point = xyz2 + num * xyz.Subtract(xyz2).Normalize();
			XYZ endOrDirection = this.GetWallPoint(wall)[0].Subtract(this.GetWallPoint(wall)[1]).Normalize();
			return this.doc.CreatYJKLine(point, endOrDirection, false);
		}

		private XYZ GetPipeWorkPlanPoint()
		{
			XYZ hyendPoint = (this.riserPipe.Location as LocationCurve).Curve.GetEndPoint(0);
			return new XYZ(hyendPoint.X, hyendPoint.Y, this.zValue);
		}

		private List<XYZ> GetWallPoint(Wall wall)
		{
			LocationCurve locationCurve = wall.Location as LocationCurve;
			XYZ xyz = locationCurve.Curve.GetEndPoint(0);
			xyz = new XYZ(xyz.X, xyz.Y, this.zValue);
			XYZ xyz2 = locationCurve.Curve.GetEndPoint(1);
			xyz2 = new XYZ(xyz2.X, xyz2.Y, this.zValue);
			return new List<XYZ>
			{
				xyz,
				xyz2
			};
		}

		private Line GetWallLine(List<XYZ> listPnt)
		{
			XYZ endOrDirection = listPnt[0].Subtract(listPnt[1]).Normalize();
			return this.doc.CreatYJKLine(listPnt[0], endOrDirection, false);
		}

		private XYZ GetFIConnPnt(FamilyInstance fi, ref double diameter)
		{
			Connector elementOutConnector = YJKRevitTools.GetElementOutConnector(fi, (Autodesk.Revit.DB.Domain)3);
			diameter = 2.0 * elementOutConnector.Radius;
			return new XYZ(elementOutConnector.Origin.X, elementOutConnector.Origin.Y, this.zValue);
		}

		private XYZ[] GetPipePoint(Pipe pipe)
		{
			Curve curve = (pipe.Location as LocationCurve).Curve;
			return new XYZ[]
			{
				curve.GetEndPoint(0),
				curve.GetEndPoint(1)
			};
		}

		private XYZ GetLineProjectPnt(Line line, XYZ point, ref double distance)
		{
			XYZ xyzpoint = line.Project(point).XYZPoint;
			distance = point.DistanceTo(xyzpoint);
			return xyzpoint;
		}

		private IList<Element> listElem;

		private Document doc;

		private Pipe riserPipe;

		private XYZ pipePnt;

		private double offset = -1100.0;

		private double slope = 0.012;

		private double pipeDiameter = 100.0;

		private double zValue;

		private PipeType pipeType;

		private PipingSystemType psType;

		private FamilySymbol fsTee;

		private bool isDownTee;

		private IList<FamilyInstance> listFamilyInstance = new List<FamilyInstance>();
	}
}
